home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 18 / develop 18 code / OSA Sample / Sources / ScriptUtils.cp < prev    next >
Encoding:
Text File  |  1994-01-28  |  11.7 KB  |  523 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ScriptUtils.cp
  3.  
  4.     Contains:    Script handling & OSA interface
  5.  
  6.  
  7.     Copyright:    Apple Computer Inc, all rights reserved.
  8.     
  9.     The routines in this file are drawn from previous DTS exampleware.
  10.  
  11. */
  12.  
  13.  
  14. #include "ScriptUtils.h"
  15. #include "SimpliFaceCommon.h"
  16.  
  17. #include    <StdArg.h>
  18. #include <Sysequ.h>
  19.  
  20. #ifndef __LIMITS__
  21. #include <Limits.h>
  22. #endif
  23.  
  24. #ifndef __ERRORS__
  25. #include <Errors.h>
  26. #endif
  27.  
  28. #ifndef __RESOURCES__
  29. #include <Resources.h>
  30. #endif
  31.  
  32. #ifndef __PACKAGES__
  33. #include <Packages.h>
  34. #endif
  35.  
  36. #ifndef __PROCESSES__
  37. #include <Processes.h>
  38. #endif
  39.  
  40. #ifndef __PLSTRINGFUNCS__
  41. #include <PLStringFuncs.h>
  42. #endif
  43.  
  44. #ifndef __AEOBJECTS__
  45. #include <AEObjects.h>
  46. #endif
  47.  
  48.  
  49. #pragma trace off
  50.  
  51.  
  52.  
  53. pascal OSErr GetCStringFromDescriptor(const AEDesc *sourceDesc, CStr255 *resultStr)
  54.   {
  55.           OSErr       err;
  56.         Size        stringSize = 0;
  57.         AEDesc      resultDesc;
  58.         char        buff[260];
  59.  
  60.         resultDesc.dataHandle = nil;
  61.         
  62.         *resultStr = "";
  63.         
  64.         err = AECoerceDesc(sourceDesc, typeChar, &resultDesc);
  65.         
  66.         if (!err) 
  67.             GetRawDataFromDescriptor(&resultDesc, (Ptr)buff,
  68.                                      255, &stringSize);
  69.         if (stringSize<256)
  70.         {
  71.             buff[stringSize] = 0;
  72.             *resultStr = buff;
  73.         }
  74.         else
  75.             err = errAECoercionFail;
  76.             
  77.         if (resultDesc.dataHandle) 
  78.             AEDisposeDesc(&resultDesc);
  79.             
  80.         return err;
  81.     }
  82.  
  83.  
  84.  
  85. OSErr    MakeNullDesc(AEDesc *dataDesc)
  86. {
  87.     return AECreateDesc(typeNull, NULL, 0, dataDesc);
  88. }
  89.  
  90.  
  91. OSErr    MakeNameDesc(CStr255& theString, AEDesc *dataDesc)
  92. {
  93.     return AECreateDesc(typeChar, (char*)theString, 
  94.                         theString.Length(), dataDesc);
  95. }
  96.  
  97.  
  98. OSErr    MakeLongIntDesc(long theIndex, AEDesc *dataDesc)
  99. {
  100.     return AECreateDesc(typeLongInteger, &theIndex, sizeof(long), dataDesc);
  101. }
  102.  
  103.  
  104.  
  105. //----------------------------------------------------------------------------------//
  106. //    Initializes all descriptor records passed in to this routine.  The variable        //
  107. //    argument list is null terminated.                                                //
  108. //----------------------------------------------------------------------------------//
  109. #pragma segment Utils
  110. void InitAEDescs(AEDesc*  desc1, ... )            // Variable, null terminated argument list.
  111. {
  112.     va_list        argptr;                            // pointer to each argument in list.
  113.     AEDesc*        nextDesc;                        // next descriptor argument in list.
  114.  
  115.     va_start(argptr, desc1);
  116.     desc1->descriptorType = typeNull;
  117.     desc1->dataHandle = nil;
  118.     
  119.     while((nextDesc = va_arg(argptr, AEDesc *)))
  120.     {
  121.         nextDesc->descriptorType = typeNull;
  122.         nextDesc->dataHandle = nil;
  123.     }
  124.     va_end(argptr);
  125. }
  126.     
  127. //----------------------------------------------------------------------------------//
  128. //    Dispose all descriptor records passed into this routine. (Variable arg. list).    //
  129. //----------------------------------------------------------------------------------//
  130. #pragma segment Utils
  131. void DisposeAEDescs(AEDesc*  desc1, ... )        // Null terminated argument list.
  132. {
  133.     va_list        argptr;            // pointer to each argument in list.
  134.     AEDesc*        nextDesc;        // descriptor argument in list.
  135.  
  136.     va_start(argptr, desc1);
  137.     if (desc1->dataHandle)
  138.         AEDisposeDesc(desc1);
  139.  
  140.     while((nextDesc = va_arg(argptr, AEDesc *)))
  141.     {
  142.         if (nextDesc->dataHandle)
  143.             AEDisposeDesc(nextDesc);
  144.     }
  145.     va_end(argptr);
  146. }
  147.  
  148. /**-----------------------------------------------------------------------
  149.         Name:             LesserOf
  150.         Purpose:        Returns the Lesser of two longints.
  151.     -----------------------------------------------------------------------**/
  152. #pragma segment Utils
  153.         
  154. pascal long LesserOf(long A, long B)
  155.  {
  156.    if (A<B)
  157.        return(A);
  158.      else
  159.        return(B);
  160.  }   /*LesserOf*/
  161.             
  162. /**-----------------------------------------------------------------------
  163.         Name:             GreaterOf
  164.         Purpose:        Returns the Greater of two longints.
  165.     -----------------------------------------------------------------------**/
  166.     
  167. #pragma segment Utils
  168.         
  169. pascal long GreaterOf(long A, long B)
  170.  {
  171.    if (A>B)
  172.        return(A);
  173.      else
  174.        return(B);
  175.  }  /*GreaterOf*/
  176.             
  177.  
  178. /**-----------------------------------------------------------------------
  179.     Utility Routines for getting data from AEDesc's
  180.   -----------------------------------------------------------------------**/
  181.     
  182. pascal void GetRawDataFromDescriptor(const AEDesc *theDesc,
  183.                                                                          Ptr     destPtr,
  184.                                                                          Size    destMaxSize,
  185.                                                                          Size    *actSize)
  186.   {
  187.       Size copySize;
  188.  
  189.  
  190.         if (theDesc->dataHandle) 
  191.             {
  192.                 HLock((Handle)theDesc->dataHandle);
  193.                 *actSize = GetHandleSize((Handle)theDesc->dataHandle);
  194.                 
  195.                 copySize = LesserOf(*actSize, destMaxSize);
  196.                 
  197.                 BlockMove(*theDesc->dataHandle, destPtr, copySize);
  198.                 
  199.                 HUnlock((Handle)theDesc->dataHandle);
  200.             }
  201.         else
  202.             *actSize = 0;
  203.     } /*GetRawDataFromDescriptor*/
  204.  
  205. pascal OSErr GetPStringFromDescriptor(const AEDesc *sourceDesc, char *resultStr)
  206.  
  207.   {
  208.       OSErr        myErr;
  209.         OSErr                 ignoreErr;
  210.         Size         stringSize;
  211.         AEDesc       resultDesc;
  212.  
  213.         resultDesc.dataHandle = nil;
  214.         
  215.         resultStr[0] = 0;
  216.         
  217.         myErr = AECoerceDesc(sourceDesc,typeChar,&resultDesc);
  218.         
  219.         if (myErr==noErr) 
  220.             GetRawDataFromDescriptor(&resultDesc,
  221.                                                              (Ptr)&resultStr[1],
  222.                                                              250,
  223.                                                              &stringSize);
  224.         if (stringSize<250) 
  225.             resultStr[0] = (char)stringSize;
  226.         else
  227.             resultStr[0] = (char)250;
  228.             
  229.         if (resultDesc.dataHandle) 
  230.             ignoreErr = AEDisposeDesc(&resultDesc);
  231.             
  232.         return(myErr);
  233.     }
  234.  
  235. pascal OSErr GetIntegerFromDescriptor(const AEDesc *sourceDesc, short *result)
  236.   {
  237.       OSErr   myErr;
  238.         OSErr   ignoreErr;
  239.         Size    intSize;
  240.         AEDesc  resultDesc;
  241.         
  242.         *result = 0;
  243.         myErr  = AECoerceDesc(sourceDesc,typeShortInteger,&resultDesc);
  244.         
  245.         if (myErr==noErr) 
  246.             {
  247.                 GetRawDataFromDescriptor(&resultDesc,
  248.                                                                  (Ptr)result,
  249.                                                                  2,
  250.                                                                  &intSize);
  251.                 if (intSize>2) 
  252.                     myErr = errAECoercionFail;
  253.             }
  254.         
  255.         if (resultDesc.dataHandle) 
  256.             ignoreErr = AEDisposeDesc(&resultDesc);
  257.             
  258.         return(myErr);
  259.     }
  260.     
  261.     
  262. pascal OSErr GetDescTypeFromDescriptor(const AEDesc *sourceDesc, DescType *result)
  263.   {
  264.       OSErr   myErr;
  265.         OSErr   ignoreErr;
  266.         Size    descSize;
  267.         AEDesc  resultDesc;
  268.         
  269.         *result = typeNull;
  270.         myErr  = AECoerceDesc(sourceDesc,typeShortInteger,&resultDesc);
  271.         
  272.         if (myErr==noErr) 
  273.             {
  274.                 GetRawDataFromDescriptor(&resultDesc,
  275.                                                                  (Ptr)result,
  276.                                                                  sizeof(DescType),
  277.                                                                  &descSize);
  278.                 if (descSize != sizeof(DescType)) 
  279.                     myErr = errAECoercionFail;
  280.             }
  281.         
  282.         if (resultDesc.dataHandle) 
  283.             ignoreErr = AEDisposeDesc(&resultDesc);
  284.             
  285.         return(myErr);
  286.     }
  287.     
  288. pascal OSErr GetBooleanFromDescriptor(const AEDesc *sourceDesc,
  289.                                                                         Boolean *result)
  290.   {
  291.       OSErr  myErr;
  292.         OSErr  ignoreErr;
  293.         Size   boolSize;
  294.         AEDesc resultDesc;
  295.         
  296.         *result = false;
  297.         myErr = AECoerceDesc(sourceDesc,typeBoolean,&resultDesc);
  298.         
  299.         if (myErr==noErr) 
  300.             {
  301.                 GetRawDataFromDescriptor(&resultDesc,
  302.                                                                  (Ptr)result,
  303.                                                                  sizeof(Boolean),
  304.                                                                  &boolSize);
  305.                 if (boolSize>sizeof(Boolean)) 
  306.                     myErr = errAECoercionFail;
  307.             }
  308.         
  309.         if (resultDesc.dataHandle) 
  310.             ignoreErr = AEDisposeDesc(&resultDesc);
  311.             
  312.         return(myErr);
  313.     }
  314.  
  315. pascal OSErr GetLongIntFromDescriptor(const AEDesc *sourceDesc, 
  316.                                       long   *result)
  317.   {
  318.       OSErr   myErr;
  319.         OSErr   ignoreErr;
  320.         Size    intSize;
  321.         AEDesc  resultDesc;
  322.         
  323.         *result = 0;
  324.         myErr = AECoerceDesc(sourceDesc,typeLongInteger,&resultDesc);
  325.         
  326.         if (myErr==noErr) 
  327.             {
  328.                 GetRawDataFromDescriptor(&resultDesc,
  329.                                                                  (Ptr)result,
  330.                                                                  4,
  331.                                                                  &intSize);
  332.                 if (intSize>4) 
  333.                     myErr = errAECoercionFail;
  334.             }
  335.         
  336.         if (resultDesc.dataHandle) 
  337.             ignoreErr = AEDisposeDesc(&resultDesc);
  338.             
  339.         return(myErr);
  340.     } /*GetLongIntFromDescriptor*/
  341.  
  342. pascal OSErr GetRectFromDescriptor(const AEDesc *sourceDesc, Rect *result)
  343.     {
  344.         OSErr   myErr;
  345.         OSErr   ignoreErr;
  346.         Size    rectSize;
  347.         AEDesc  resultDesc;
  348.             
  349.         SetRect(result,0,0,0,0);
  350.         myErr = AECoerceDesc(sourceDesc,typeQDRectangle,&resultDesc);
  351.         
  352.         if (myErr==noErr) 
  353.             {
  354.                 GetRawDataFromDescriptor(&resultDesc,
  355.                                                                  (Ptr)result,
  356.                                                                  sizeof(Rect),
  357.                                                                  &rectSize);
  358.                 if (rectSize<sizeof(Rect)) 
  359.                     myErr = errAECoercionFail;
  360.             }
  361.         
  362.         if (resultDesc.dataHandle) 
  363.             ignoreErr = AEDisposeDesc(&resultDesc);
  364.             
  365.         return(myErr);
  366.     } /*GetRectFromDescriptor*/
  367.  
  368. pascal OSErr GetPointFromDescriptor(const AEDesc *sourceDesc,
  369.                                                                   Point  *result)
  370.   {
  371.       OSErr   myErr;
  372.         OSErr   ignoreErr;
  373.         Size    ptSize;
  374.         AEDesc  resultDesc;
  375.         
  376.         SetPt(result,0,0);
  377.         
  378.         myErr = AECoerceDesc(sourceDesc,typeQDPoint,&resultDesc);
  379.         
  380.         if (myErr==noErr) 
  381.             {
  382.                 GetRawDataFromDescriptor(&resultDesc,
  383.                                                                  (Ptr)result,
  384.                                                                  sizeof(Point),
  385.                                                                  &ptSize);
  386.                                                                  
  387.                 if (ptSize<sizeof(Point)) 
  388.                     myErr = errAECoercionFail;
  389.                     
  390.             }
  391.         
  392.         if (resultDesc.dataHandle) 
  393.             ignoreErr = AEDisposeDesc(&resultDesc);
  394.             
  395.         return(myErr);
  396.     } /*GetPointFromDescriptor*/
  397.  
  398.  
  399. /*******************************************************************************/
  400. /*
  401.     Object Accessors - Utility Routines
  402. */
  403.  
  404. #pragma segment ObjectAccessors
  405.  
  406. pascal WindowPtr WindowNameToWindowPtr(StringPtr nameStr)
  407. /* 
  408.     Returns the WindowPtr of the window with title nameStr
  409.     or nil if there is no matching window.
  410. */
  411.     { 
  412.         WindowPtr theWindow;
  413.         Str255    windTitle;
  414.             
  415.         theWindow = (WindowPtr)*((Handle)WindowList);
  416.         /* 
  417.             iterate through windows - we use WindowList 'cos we could
  418.             have made the window invisible and  we lose it - so we
  419.             can't set it back to visible!!
  420.         */
  421.         while (theWindow)
  422.             {
  423.                 GetWTitle(theWindow, windTitle);
  424.                 if (EqualString(windTitle,
  425.                                 nameStr,
  426.                                                 false,
  427.                                                 true))     /* ignore case, don't ignore diacriticals */
  428.                     return(theWindow);
  429.               theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  430.             }
  431.         return(theWindow);
  432.     }    /* WindowNameToWindowPtr */
  433.  
  434. pascal WindowPtr GetWindowPtrOfNthWindow(short index)
  435. /* returns a ptr to the window with the given index
  436.   (front window is 1, behind that is 2, etc.).  if
  437.   there's no window with that index (inc. no windows
  438.   at all), returns nil.
  439. */
  440.   {
  441.       WindowPtr theWindow;
  442.         
  443.         theWindow = (WindowPtr)*((Handle)WindowList);
  444.     
  445.         /* iterate through windows */
  446.         
  447.         while (theWindow)
  448.             {
  449.                 index --;
  450.                 if (index <= 0) 
  451.                     return(theWindow);
  452.                     
  453.               theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  454.             }
  455.         return(nil);
  456.     }    /* GetWindowPtrOfNthWindow */
  457.     
  458. pascal short CountWindows(void)
  459.     {
  460.         WindowPtr theWindow;
  461.         short     index;
  462.                 
  463.         index = 0;
  464.         theWindow = (WindowPtr)*((Handle)WindowList);
  465.     
  466.         /* iterate through windows */
  467.         
  468.         while (theWindow)
  469.             {
  470.                 index++;                    
  471.               theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  472.             }
  473.         
  474.         return(index);
  475.         
  476.     } /*CountWindows*/
  477.  
  478.  
  479.     
  480. /* Next bit of code nabbed from SignatureToApp */
  481.  
  482. pascal OSErr FindApplicationOnVolume(OSType whatSig, short theVol, FSSpec *foundSpec)
  483.     {
  484.       OSErr                  myErr;
  485.         DTPBRec                myPB;
  486.         HParamBlockRec         myHParams;
  487.         GetVolParmsInfoBuffer  theVolInfo;
  488.         
  489.         myHParams.ioParam.ioCompletion = nil;
  490.         myHParams.ioParam.ioNamePtr    = nil;
  491.         myHParams.ioParam.ioVRefNum    = theVol;
  492.         myHParams.ioParam.ioBuffer     = (Ptr)&theVolInfo;
  493.         myHParams.ioParam.ioReqCount   = sizeof(theVolInfo);
  494.         
  495.         myErr = PBHGetVolParms(&myHParams, false); /* synchchronous */
  496.  
  497.         if (myErr==noErr && (theVolInfo.vMAttrib & (1 << bHasDesktopMgr)) != 0)
  498.             { /* Have a deskTop database */
  499.                 myPB.ioCompletion = nil;
  500.                 myPB.ioVRefNum    = theVol;
  501.                 myPB.ioNamePtr    = nil;
  502.                 
  503.                 myErr = PBDTGetPath(&myPB); /* Gets myPB.ioDTRefNum */
  504.                 
  505.                 if (myErr==noErr)
  506.                   {
  507.                         myPB.ioCompletion = nil;
  508.                         myPB.ioIndex      = 0;
  509.                         myPB.ioFileCreator= whatSig;
  510.                         myPB.ioNamePtr    = (StringPtr)&foundSpec->name[0];
  511.                         
  512.                         myErr = PBDTGetAPPL(&myPB, false);
  513.                         
  514.                         if (myErr == noErr)
  515.                             {
  516.                                 foundSpec->vRefNum = theVol;
  517.                                 foundSpec->parID   = myPB.ioAPPLParID;
  518.                             }
  519.                   }                
  520.             }        
  521.         return(myErr);
  522.     }
  523.